# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=cvxopt
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.2.3
pkgrel=2
pkgdesc="Convex optimization based on the Python programming language (mingw-w64)"
arch=('any')
url='https://cvxopt.org/'
license=('GPL v3')
validpgpkeys=('gpg_KEY')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
depends=("${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-dsdp"
         "${MINGW_PACKAGE_PREFIX}-glpk")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/${_realname}/${_realname}/archive/${pkgver}.tar.gz")
sha256sums=('9a117022984586ecc86ab643da74fcab2411b9c43a26f1b9a9d0bd9ed26728ce')

prepare() {
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done
}

build() {
  for pver in {2,3}; do
    msg "Python ${pver} build for ${CARCH}"
    cd "${srcdir}/python${pver}-build-${CARCH}"
    CVXOPT_BLAS_LIB='openblas'                          \
    CVXOPT_LAPACK_LIB='openblas'                        \
    CVXOPT_BUILD_GSL=1                                  \
    CVXOPT_BUILD_FFTW=1                                 \
    CVXOPT_BUILD_DSDP=1                                 \
    CVXOPT_BUILD_GLPK=1                                 \
    CVXOPT_GSL_LIB_DIR=${MINGW_PREFIX}/lib              \
    CVXOPT_GSL_INC_DIR=${MINGW_PREFIX}/include          \
    CVXOPT_FFTW_LIB_DIR=${MINGW_PREFIX}/lib             \
    CVXOPT_FFTW_INC_DIR=${MINGW_PREFIX}/include         \
    CVXOPT_GLPK_LIB_DIR=${MINGW_PREFIX}/lib             \
    CVXOPT_GLPK_INC_DIR=${MINGW_PREFIX}/include         \
    CVXOPT_DSDP_LIB_DIR=${MINGW_PREFIX}/lib             \
    CVXOPT_DSDP_INC_DIR=${MINGW_PREFIX}/include/dsdp    \
    CVXOPT_SUITESPARSE_LIB_DIR=${MINGW_PREFIX}/lib      \
    CVXOPT_SUITESPARSE_INC_DIR=${MINGW_PREFIX}/include  \
      ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done
}

check() {
  for pver in {2,3}; do
    msg "Python ${pver} test for ${CARCH}"
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py check
  done
}

package_python3-cvxopt() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  cd "${srcdir}"/python3-build-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
}

package_python2-cvxopt() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  cd "${srcdir}"/python2-build-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
}

package_mingw-w64-i686-python2-cvxopt() {
  package_python2-cvxopt
}

package_mingw-w64-i686-python3-cvxopt() {
  package_python3-cvxopt
}

package_mingw-w64-x86_64-python2-cvxopt() {
  package_python2-cvxopt
}

package_mingw-w64-x86_64-python3-cvxopt() {
  package_python3-cvxopt
}
